#ifndef __MUTEXLOCK_H_
#define __MUTEXLOCK_H_
#include<pthread.h>
#include<iostream>
#include"Noncopyable.h"
#include "../../../log/Mylog.h"

namespace SE
{
    class Mutexlock : Noncopyable
    {
    public:
        Mutexlock()
        : _isLocked(false)
        {
            if(pthread_mutex_init(&_mutex, NULL))
                logError("pthread_mutex_init");
        }
        ~Mutexlock()
        {
            if(pthread_mutex_destroy(&_mutex))
                logError("pthread_mutex_destroy");
        }

        pthread_mutex_t* getMutexPtr()
        {
            return &_mutex;
        }

        void lock()
        {
            if(pthread_mutex_lock(&_mutex))
                logError("pthread_mutex_lock");
            _isLocked = true;
        }

        void unlock()
        {
            if(pthread_mutex_unlock(&_mutex))
                logError("pthread_mutex_unlock");
            _isLocked = false;
        }
        
        bool isLocked() const 
        {
            return _isLocked;
        }

    private:
        pthread_mutex_t _mutex;
        bool _isLocked;
    };

    class MutexGuard
    {
    public:
        MutexGuard(Mutexlock& mutex)
        : _mutex(mutex)
        {
            _mutex.lock();
        }
        ~MutexGuard()
        {
            _mutex.unlock();
        }
        
    private:
        Mutexlock& _mutex;
    };

}

#endif